package edu.hawaii.duedates.web.page.index;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; 
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Test;
import edu.hawaii.duedates.web.DueDatesApplication;
import edu.hawaii.duedates.web.Start;

/**
 * Tests the index page.
 * 
 * @author Ronn Reeves.
 */
public class TestIndex {

  /**
   * Tests the index page.
   */
  @SuppressWarnings("static-access")
  @Test
  public void testIndex() {
    Start start = new Start();
    try {
      String[] args = new String[1];
      args[0] = "test";
      start.main(args);
    }
    catch (Exception e) {
      start = new Start();
    }
    assertEquals("Start failed.", start.getClass(), Start.class);

    DueDatesApplication dda = new DueDatesApplication();
    WicketTester tester = new WicketTester(dda);    
    assertEquals("DueDates application instantiated.", (dda.get()).getClass(), 
      DueDatesApplication.class);
    tester.startPage(Index.class);
    tester.assertRenderedPage(Index.class);

    // Test the Index.html page displayed correctly.
    tester.assertContains("Welcome");

    String formName = "wuxDueDatesForm";

    // Test that the page components were rendered.
    assertNotNull("DueDates form rendered.", tester.getTagByWicketId(formName));
    assertNotNull("DueDates user id field rendered.", tester.getTagByWicketId("wuxUserId"));
    assertNotNull("DueDates password field rendered.", tester.getTagByWicketId("wuxPassword"));
    
    // Test the login operations.
    FormTester loginNullTester = tester.newFormTester(formName);
    assertEquals("DueDates username text box is empty.", "", 
      loginNullTester.getTextComponentValue("wuxUserId"));
    loginNullTester.setValue("wuxUserId", null);
    assertEquals("DueDates password is empty.", "",
      loginNullTester.getTextComponentValue("wuxPassword"));
    loginNullTester.setValue("wuxPassword", null);
//    loginNullTester.submit("wuxLogin");
//    tester.assertRenderedPage(Index.class);
  }
}